package cn.com.infcn.spider.scheduler;

import org.apache.log4j.Logger;

import cn.com.infcn.spider.URIManager;
import cn.com.infcn.spider.domain.Doc;
import cn.com.infcn.spider.domain.Job;
import cn.com.infcn.spider.io.input.Input;
import cn.com.infcn.spider.io.output.Output;

public abstract class Scheduler extends Thread {

	protected static final Logger LOG = Logger.getLogger(Scheduler.class);

	protected Job job;

	protected Output[] outputs;

	protected Input[] inputs;

	public Scheduler(Job job) {
		this.job = job;
		init();
	}

	protected abstract void init();

	public void shutdown() throws Exception {
		flush();
	}

	public void flush() throws Exception {
		for (Output output : outputs) {
			output.flush();
		}
	}

	public void drop() {
		try {
			shutdown();
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
		}
		URIManager.drop(job.getName());
	}

	/**
	 * 保存
	 * 
	 * @param job
	 */
	public void output(Doc doc) {
		for (Output output : outputs) {
			try {
				output.save(doc);
			} catch (Exception e) {
				e.printStackTrace();
				LOG.error(e);
			}
		}
	}

	public Job getJob() {
		return job;
	}

	public abstract boolean isRuning();

	public abstract Object test(String uri); // 測試接口

}
